/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

// Package helmchart
package helmchart

import (
	"bytes"
	"testing"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
)

const (
	testTargetSystem   = int32(1)
	testRepoName       = "stable"
	testChartName      = "nginx"
	testChartVersion   = "1.16.0"
	testSuccess        = true
	testFailure        = false
	testChartBytesSize = 1024
	emptyString        = ""
	zeroInt32          = int32(0)
	maxLineLength      = 120
	maxFunctionLines   = 50
	expectedDescIndex0 = 0
	expectedDescIndex1 = 1
	expectedFieldCount = 4
	expectedRPCCount   = 2
	testByteValue      = byte(0x48)
)

var (
	testChartBytes = []byte("test chart content")
	largeChartData = make([]byte, testChartBytesSize)
)

func init() {
	for i := range largeChartData {
		largeChartData[i] = testByteValue
	}
}

func createTestChartRequest() *ChartRequest {
	return &ChartRequest{
		TargetSystem: testTargetSystem,
		RepoName:     testRepoName,
		ChartName:    testChartName,
		ChartVersion: testChartVersion,
	}
}

func createEmptyChartRequest() *ChartRequest {
	return &ChartRequest{}
}

func createTestChartResponse() *ChartResponse {
	return &ChartResponse{
		Success:    testSuccess,
		ChartBytes: testChartBytes,
	}
}

func createEmptyChartResponse() *ChartResponse {
	return &ChartResponse{}
}

func createFailureChartResponse() *ChartResponse {
	return &ChartResponse{
		Success:    testFailure,
		ChartBytes: nil,
	}
}

func TestChartRequestGetTargetSystem(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	result := req.GetTargetSystem()
	if result != testTargetSystem {
		t.Errorf("Expected TargetSystem %d, got %d", testTargetSystem, result)
	}
}

func TestChartRequestGetTargetSystemNil(t *testing.T) {
	var req *ChartRequest
	if req == nil {
		t.Log("ChartRequest is nil, cannot call GetTargetSystem() safely")
		return
	}

	result := req.GetTargetSystem()
	if result != zeroInt32 {
		t.Errorf("Expected default TargetSystem %d, got %d", zeroInt32, result)
	}
}

func TestChartRequestGetRepoName(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	result := req.GetRepoName()
	if result != testRepoName {
		t.Errorf("Expected RepoName %s, got %s", testRepoName, result)
	}
}

func TestChartRequestGetRepoNameNil(t *testing.T) {
	var req *ChartRequest
	if req == nil {
		t.Log("ChartRequest is nil, cannot call GetRepoName() safely")
		return
	}

	result := req.GetRepoName()
	if result != emptyString {
		t.Errorf("Expected default RepoName %s, got %s", emptyString, result)
	}
}

func TestChartRequestGetChartName(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	result := req.GetChartName()
	if result != testChartName {
		t.Errorf("Expected ChartName %s, got %s", testChartName, result)
	}
}

func TestChartRequestGetChartNameNil(t *testing.T) {
	var req *ChartRequest
	if req == nil {
		t.Log("ChartRequest is nil, cannot call GetChartName() safely")
		return
	}

	result := req.GetChartName()
	if result != emptyString {
		t.Errorf("Expected default ChartName %s, got %s", emptyString, result)
	}
}

func TestChartRequestGetChartVersion(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	result := req.GetChartVersion()
	if result != testChartVersion {
		t.Errorf("Expected ChartVersion %s, got %s", testChartVersion, result)
	}
}

func TestChartRequestGetChartVersionNil(t *testing.T) {
	var req *ChartRequest
	if req == nil {
		t.Log("ChartRequest is nil, cannot call GetChartVersion() safely")
		return
	}

	result := req.GetChartVersion()
	if result != emptyString {
		t.Errorf("Expected default ChartVersion %s, got %s", emptyString, result)
	}
}

func TestChartRequestReset(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	req.Reset()

	if req.GetTargetSystem() != zeroInt32 {
		t.Errorf("Expected TargetSystem to be reset to %d, got %d",
			zeroInt32, req.GetTargetSystem())
	}

	if req.GetRepoName() != emptyString {
		t.Errorf("Expected RepoName to be reset to empty string, got %s", req.GetRepoName())
	}

	if req.GetChartName() != emptyString {
		t.Errorf("Expected ChartName to be reset to empty string, got %s", req.GetChartName())
	}
}

func TestChartRequestString(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	result := req.String()
	if result == emptyString {
		t.Error("String() should not return empty string")
	}

	if len(result) == 0 {
		t.Error("String() should return non-empty string representation")
	}
}

func TestChartRequestProtoReflect(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	msg := req.ProtoReflect()
	if msg == nil {
		t.Fatal("ProtoReflect() returned nil")
	}

	descriptor := msg.Descriptor()
	if descriptor == nil {
		t.Fatal("Descriptor() returned nil")
	}

	fields := descriptor.Fields()
	if fields.Len() != expectedFieldCount {
		t.Errorf("Expected %d fields, got %d", expectedFieldCount, fields.Len())
	}
}

func TestChartRequestDescriptor(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	descBytes, indices := req.Descriptor()
	if descBytes == nil {
		t.Error("Descriptor() returned nil bytes")
	}

	if len(indices) != expectedDescIndex1 {
		t.Errorf("Expected 1 indices, got %d", len(indices))
	}

	if indices[0] != expectedDescIndex0 {
		t.Errorf("Expected first index %d, got %d", expectedDescIndex0, indices[0])
	}
}

func TestChartResponseGetSuccess(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	result := resp.GetSuccess()
	if result != testSuccess {
		t.Errorf("Expected Success %v, got %v", testSuccess, result)
	}
}

func TestChartResponseGetSuccessNil(t *testing.T) {
	var resp *ChartResponse
	if resp == nil {
		t.Log("ChartResponse is nil, cannot call GetSuccess() safely")
		return
	}

	result := resp.GetSuccess()
	if result != testFailure {
		t.Errorf("Expected default Success %v, got %v", testFailure, result)
	}
}

func TestChartResponseGetChartBytes(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	result := resp.GetChartBytes()
	if !bytes.Equal(result, testChartBytes) {
		t.Errorf("Expected ChartBytes %v, got %v", testChartBytes, result)
	}
}

func TestChartResponseGetChartBytesNil(t *testing.T) {
	var resp *ChartResponse
	if resp == nil {
		t.Log("ChartResponse is nil, cannot call GetChartBytes() safely")
		return
	}

	result := resp.GetChartBytes()
	if result != nil {
		t.Errorf("Expected default ChartBytes nil, got %v", result)
	}
}

func TestChartResponseReset(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	resp.Reset()

	if resp.GetSuccess() != testFailure {
		t.Errorf("Expected Success to be reset to %v, got %v", testFailure, resp.GetSuccess())
	}

	if resp.GetChartBytes() != nil {
		t.Errorf("Expected ChartBytes to be reset to nil, got %v", resp.GetChartBytes())
	}
}

func TestChartResponseString(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	result := resp.String()
	if result == emptyString {
		t.Error("String() should not return empty string")
	}

	if len(result) == 0 {
		t.Error("String() should return non-empty string representation")
	}
}

func TestChartResponseProtoReflect(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	msg := resp.ProtoReflect()
	if msg == nil {
		t.Fatal("ProtoReflect() returned nil")
	}

	descriptor := msg.Descriptor()
	if descriptor == nil {
		t.Fatal("Descriptor() returned nil")
	}
}

func TestChartResponseDescriptor(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	descBytes, indices := resp.Descriptor()
	if descBytes == nil {
		t.Error("Descriptor() returned nil bytes")
	}

	if len(indices) != 1 {
		t.Errorf("Expected 1 indices, got %d", len(indices))
	}

	if indices[0] != expectedDescIndex1 {
		t.Errorf("Expected first index %d, got %d", expectedDescIndex1, indices[0])
	}
}

func TestChartRequestProtoMessage(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	// 验证实现了 proto.Message 接口
	var _ proto.Message = req
	req.ProtoMessage() // 应该不会panic
}

func TestChartResponseProtoMessage(t *testing.T) {
	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	// 验证实现了 proto.Message 接口
	var _ proto.Message = resp
	resp.ProtoMessage() // 应该不会panic
}

func TestChartRequestSerialization(t *testing.T) {
	original := createTestChartRequest()
	if original == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	data, err := proto.Marshal(original)
	if err != nil {
		t.Fatalf("Failed to marshal ChartRequest: %v", err)
	}

	if len(data) == 0 {
		t.Error("Marshaled data should not be empty")
	}

	var unmarshaled ChartRequest
	err = proto.Unmarshal(data, &unmarshaled)
	if err != nil {
		t.Fatalf("Failed to unmarshal ChartRequest: %v", err)
	}

	if unmarshaled.GetTargetSystem() != original.GetTargetSystem() {
		t.Errorf("Expected TargetSystem %d, got %d",
			original.GetTargetSystem(), unmarshaled.GetTargetSystem())
	}

	if unmarshaled.GetRepoName() != original.GetRepoName() {
		t.Errorf("Expected RepoName %s, got %s",
			original.GetRepoName(), unmarshaled.GetRepoName())
	}
}

func TestChartResponseSerialization(t *testing.T) {
	original := createTestChartResponse()
	if original == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	data, err := proto.Marshal(original)
	if err != nil {
		t.Fatalf("Failed to marshal ChartResponse: %v", err)
	}

	if len(data) == 0 {
		t.Error("Marshaled data should not be empty")
	}

	var unmarshaled ChartResponse
	err = proto.Unmarshal(data, &unmarshaled)
	if err != nil {
		t.Fatalf("Failed to unmarshal ChartResponse: %v", err)
	}

	if unmarshaled.GetSuccess() != original.GetSuccess() {
		t.Errorf("Expected Success %v, got %v",
			original.GetSuccess(), unmarshaled.GetSuccess())
	}

	if !bytes.Equal(unmarshaled.GetChartBytes(), original.GetChartBytes()) {
		t.Errorf("Expected ChartBytes %v, got %v",
			original.GetChartBytes(), unmarshaled.GetChartBytes())
	}
}

func TestFileDescriptorInit(t *testing.T) {
	if File_helmchart_proto == nil {
		t.Error("File_helmchart_proto should not be nil after init")
	}

	desc := File_helmchart_proto
	if desc.Path() != "helmchart.proto" {
		t.Errorf("Expected file path 'helmchart.proto', got %s", desc.Path())
	}
}

func TestRawDescriptorGZIP(t *testing.T) {
	data := file_helmchart_proto_rawDescGZIP()
	if data == nil {
		t.Error("rawDescGZIP() should not return nil")
	}

	if len(data) == 0 {
		t.Error("rawDescGZIP() should not return empty data")
	}
}

func TestChartRequestWithLargeData(t *testing.T) {
	req := &ChartRequest{
		TargetSystem: testTargetSystem,
		RepoName:     testRepoName,
		ChartName:    testChartName,
		ChartVersion: testChartVersion,
	}

	if req.GetTargetSystem() != testTargetSystem {
		t.Errorf("Expected TargetSystem %d, got %d", testTargetSystem, req.GetTargetSystem())
	}
}

func TestChartResponseWithLargeData(t *testing.T) {
	resp := &ChartResponse{
		Success:    testSuccess,
		ChartBytes: largeChartData,
	}

	if resp.GetSuccess() != testSuccess {
		t.Errorf("Expected Success %v, got %v", testSuccess, resp.GetSuccess())
	}

	if len(resp.GetChartBytes()) != testChartBytesSize {
		t.Errorf("Expected ChartBytes length %d, got %d",
			testChartBytesSize, len(resp.GetChartBytes()))
	}
}

func TestEmptyRequestsAndResponses(t *testing.T) {
	emptyReq := createEmptyChartRequest()
	if emptyReq == nil {
		t.Fatal("Failed to create empty ChartRequest")
	}

	if emptyReq.GetTargetSystem() != zeroInt32 {
		t.Errorf("Empty request TargetSystem should be %d, got %d",
			zeroInt32, emptyReq.GetTargetSystem())
	}

	emptyResp := createEmptyChartResponse()
	if emptyResp == nil {
		t.Fatal("Failed to create empty ChartResponse")
	}

	if emptyResp.GetSuccess() != testFailure {
		t.Errorf("Empty response Success should be %v, got %v",
			testFailure, emptyResp.GetSuccess())
	}
}

func TestFailureResponse(t *testing.T) {
	failResp := createFailureChartResponse()
	if failResp == nil {
		t.Fatal("Failed to create failure ChartResponse")
	}

	if failResp.GetSuccess() != testFailure {
		t.Errorf("Failure response Success should be %v, got %v",
			testFailure, failResp.GetSuccess())
	}

	if failResp.GetChartBytes() != nil {
		t.Errorf("Failure response ChartBytes should be nil, got %v",
			failResp.GetChartBytes())
	}
}

func TestProtoReflectInterface(t *testing.T) {
	req := createTestChartRequest()
	if req == nil {
		t.Fatal("Failed to create test ChartRequest")
	}

	var _ protoreflect.ProtoMessage = req

	resp := createTestChartResponse()
	if resp == nil {
		t.Fatal("Failed to create test ChartResponse")
	}

	var _ protoreflect.ProtoMessage = resp
}
